﻿@inject ITechnologyService TechnologyService
@inject IStandardBomService StandardBomService
@inject IProductionPlanService ProductionPlanService

<RadzenStack>
    <RadzenStack>
        <RadzenRow>
            <RadzenColumn>
                <RadzenButton Variant="Variant.Outlined" Click="SubmitHandle">保存</RadzenButton>
                <RadzenButton Variant="Variant.Outlined" Click="CancelHandle">取消</RadzenButton>
            </RadzenColumn>
        </RadzenRow>
    </RadzenStack>
    <RadzenStack>
        <RadzenFieldset AllowCollapse="true" Style="width: 100%; margin: 40px auto;" ExpandTitle="Expand orders." CollapseTitle="Collapse orders."
                        ExpandAriaLabel="Expand the list of orders." CollapseAriaLabel="Collapse the list of orders.">
            <HeaderTemplate>
                <span class="d-inline-flex align-items-center align-middle">
                    <RadzenIcon Icon="account_box" class="me-1"/><b>基本信息</b>
                </span>
            </HeaderTemplate>
            <ChildContent>
                <RadzenStack class="rz-mb-2">
                    <RadzenRow>
                        <RadzenColumn Size="12" SizeSM="6">
                            <RadzenFormField Text="计划编码" Style="width: 100%;">
                                <RadzenTextBox @bind-Value="_editRequestDto.Code"></RadzenTextBox>
                            </RadzenFormField>
                        </RadzenColumn>
                        <RadzenColumn Size="12" SizeSM="6">
                            <RadzenFormField Text="计划名称" Style="width: 100%;">
                                <RadzenTextBox @bind-Value="_editRequestDto.Name"></RadzenTextBox>
                            </RadzenFormField>
                        </RadzenColumn>
                        <RadzenColumn Size="12" SizeSM="6">
                            <RadzenFormField Text="BOM编码" Style="width: 100%;">
                                <RadzenDropDownDataGrid @bind-Value=_editRequestDto.StandardBom.Id LoadData=@LoadStandardBomData AllowFiltering="true"
                                                        Data="_standardBoms" Count="_standardCount"
                                                        TextProperty="Code" ValueProperty="Id">
                                    <Columns>
                                        <RadzenDropDownDataGridColumn Property="Code" Title="Code" Width="100px"/>
                                        <RadzenDropDownDataGridColumn Property="Name" Title="Name" Width="100px"/>
                                    </Columns>
                                </RadzenDropDownDataGrid>
                            </RadzenFormField>
                        </RadzenColumn>
                    </RadzenRow>
                </RadzenStack>
            </ChildContent>
        </RadzenFieldset>
    </RadzenStack>
</RadzenStack>

@code
{
    private ProductionPlanEditRequestDto _editRequestDto = new();
    private IList<StandardBomInfoResponseDto> _standardBoms = new List<StandardBomInfoResponseDto>();
    private int _standardCount = 0;
    
    async Task SubmitHandle()
    {
        await ProductionPlanService.CreateProductionPlan(_editRequestDto);
    }

    void CancelHandle()
    {
    }
    
    async Task LoadStandardBomData(LoadDataArgs args)
    {
        var result = await StandardBomService.QueryStandardBomsByPage(new StandardBomSearchRequestDto());
        if (result != null && result.Succeeded)
        {
            _standardBoms = result.Data!;
            _standardCount = result.TotalCount;
        }

        await InvokeAsync(StateHasChanged);
    }
    
}

@* @inject ITechnologyService TechnologyService
@inject IStandardBomService StandardBomService
@inject IProductionPlanService ProductionPlanService

<MudStack>
    <MudToolBar>
        <MudSpacer></MudSpacer>
        <MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="SubmitHandle"
                   Size="Size.Small" Class="mr-5">
            保存
        </MudButton>
        <MudButton Variant="Variant.Filled" OnClick="CancelHandle"
                   Size="Size.Small">
            取消
        </MudButton>
    </MudToolBar>
    <MudPaper Class="pa-4" Elevation="0">
        <MudText Color="Color.Primary" Typo="Typo.h6">基本信息</MudText>
        <MudForm @ref="_editRequestForm" Model="_editRequestDto"
                 ValidationDelay="0">
            <MudGrid>
                <MudItem xs="12" md="6">
                    <MudTextField @bind-Value="" For="@(() => _editRequestDto.Code)" Label=""
                                  Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudTextField>
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudTextField @bind-Value="" For="@(() => _editRequestDto.Name)" Label=""
                                  Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudTextField>
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudAutocomplete T="StandardBomInfoResponseDto"
                                     Label="产品编码" @bind-Value="_editRequestDto.StandardBom"
                                     SearchFunc="@StandardBomSearch" ToStringFunc="@(e => e?.Code)"
                                     Dense="true" Margin="Margin.Dense" Variant="Variant.Outlined" />
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudAutocomplete T="TechnologyInfoResponseDto"
                                     Label="工艺编码" @bind-Value="_editRequestDto.Technology"
                                     SearchFunc="@TechnologySearch" ToStringFunc="@(e => e?.Code)"
                                     Dense="true" Margin="Margin.Dense" Variant="Variant.Outlined" />
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudNumericField @bind-Value="_editRequestDto.PriorityLevel" For="@(() => _editRequestDto.PriorityLevel)" Label="优先级"
                                     Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudNumericField>
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudSelect @bind-Value="_editRequestDto.ScheduleType" For="(() => _editRequestDto.ScheduleType)"
                               Label="排程类型" Dense="true"
                               AnchorOrigin="Origin.BottomCenter"
                               Variant="Variant.Outlined" Margin="Margin.Dense" Clearable="true">
                        <MudSelectItem Value="1">正排</MudSelectItem>
                        <MudSelectItem Value="2">倒排</MudSelectItem>
                    </MudSelect>
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudNumericField @bind-Value="_editRequestDto.Quality" For="@(() => _editRequestDto.Quality)" Label="生产产量"
                                     Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudNumericField>
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudDatePicker Label="预期开始时间" @bind-Date="_editRequestDto.ExpectStartDate" Editable="true"
                                   Variant="Variant.Outlined" DisableToolbar="true" Margin="Margin.Dense" />
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudDatePicker Label="预期结束时间" @bind-Date="_editRequestDto.ExpectEndDate" Editable="true"
                                   Variant="Variant.Outlined" DisableToolbar="true" Margin="Margin.Dense" />
                </MudItem>
                <MudItem xs="12">
                    <MudTextField @bind-Value="_editRequestDto.Description" For="@(() => _editRequestDto.Description)"
                                  Label="计划描述"
                                  Lines="3" Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudTextField>
                </MudItem>
            </MudGrid>
        </MudForm>
    </MudPaper>

</MudStack>


@code {
    

    private MudForm _editRequestForm = new();

   

    private async Task<IEnumerable<StandardBomInfoResponseDto>> StandardBomSearch(string value)
    {
        var result = await StandardBomService.QueryStandardBoms(new StandardBomSearchRequestDto());
        if (result != null && result.Succeeded)
        {
            return result.Data!;
        }
        return new List<StandardBomInfoResponseDto>();
    }

    private async Task<IEnumerable<TechnologyInfoResponseDto>> TechnologySearch(string value)
    {
        var result = await TechnologyService.QueryTechnologies(new ProcessSearchRequestDto());
        if (result != null && result.Succeeded)
        {
            return result.Data!;
        }
        return new List<TechnologyInfoResponseDto>();
    }

} *@